<template>
  <UserOption></UserOption>
  <ProductInventory :ProductInventoryArr="State.ProductInventoryArr"></ProductInventory>
</template>

<script>
import ProductInventory from "@/components/produc/ProductInventory.vue";
import { postData } from "@/assets/js/pubilcs.js";
import { defineComponent, reactive, onMounted,toRef, toRefs } from "vue";
export default defineComponent({
  name: "ProductLibrary",
  components: {
    ProductInventory
  },
  setup() {
    let State = reactive({
        ProductInventoryArr:[]
    })
    //获取所有产品
    function GetAllProduct(){
      if(sessionStorage.getItem("ProductInventoryArr")){
        State.ProductInventoryArr = JSON.parse(sessionStorage.getItem("ProductInventoryArr"))
      }
      else{
        let schoolId= sessionStorage.getItem("schoolId")
        let userid = sessionStorage.getItem("userid")
        postData(
        "/api/Product/GetAllBySchoolId",
        JSON.stringify({SchoolId:schoolId,UserId:userid}),
        sessionStorage.getItem("token")
        )
        .then(function (res) {
          //储存产品
          State.ProductInventoryArr = res.result.result
          //临时储存所有产品,第二次用sessionStorage获取
          sessionStorage.setItem("ProductInventoryArr",JSON.stringify(State.ProductInventoryArr))
        })
        .catch((error) => {

        });
      }
    }
    onMounted(() => {
      GetAllProduct()
    });
    return {
      State,
      GetAllProduct
    };
  },
});
</script>
<style lang="less">

</style>
